// appi.js
const express=require('express');
const path=require('path');
const url = require('url');
// const {getImages} = require('./getimages');

const app = express();

const connection = require('./db');

const getImages = (callback,params) => {
    let sql = 'SELECT id,`timestamp`,picture_path FROM `sensor_data` where router_number= ? and picture_path <> ? ORDER BY id DESC  LIMIT 10';
    // const params = ['02','0']
    connection.query(sql, params,(error, results, fields) => {
        callback(error,results,fields);
    });
};

app.use('/api/images',express.static(path.join('D:', 'picture')));


const handleResults = (error,images,fields) => {
    let imagines;
    imagines = images.map(image => {
        return path.basename(image.picture_path);
    });
    console.log(imagines);
    return imagines;
}

// const images = await getImages();


const params = ['64','0'];

// 获取所有用户并打印结果
getImages(handleResults,params);

app.get('/api/images', function (req, res) {
    const parsedUrl = url.parse(req.url, true);
    const pathname = parsedUrl.pathname;
    const filename = path.basename(pathname);
    res.sendFile(filename);
})


app.listen(8080, () => {
    console.log("Server started on port 8080");
});
